package com.qf.zyh.web.controller;

import com.github.pagehelper.PageInfo;
import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Emp;
import com.qf.zyh.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpServices es;

    @RequestMapping("/login")
    @ResponseBody
    public String login(String ename,String pwd){
        Emp e = es.login(ename, pwd);
        if (e!=null) {
            return Const.SUCCESS;
        }
        return Const.FAILE;
    }
    @RequestMapping("/index")
    public String getAll(Model m,@RequestParam(defaultValue = "1") Integer pageIndex){
        PageInfo<Emp> empByPage = es.getEmpByPage(pageIndex);
        m.addAttribute("pageInfo",empByPage);
        return "index";
    }
    @RequestMapping("/addEmp")
    public String addEmp(Emp emp){
        boolean b = es.insertEmp(emp);
            return "redirect:/emp/index";
    }
    @RequestMapping("/updateEmp")
    public String updateEmp(Integer eno,Model m){
        Emp e = es.getEmpByEno(eno);
        System.out.println(e.getEsex());
        m.addAttribute("Emp",e);
        return "updateEmp";
    }
    @RequestMapping("/del")
    public String delEmp(Integer eno){
        boolean b = es.deleteEmoByEno(eno);
        return "redirect:/emp/index";
    }
    @RequestMapping("/updaEmp")
    public String updaEmp(Emp emp){
        boolean b = es.updateEmp(emp);
        return "redirect:/emp/index";
    }
}
